import { Record, List } from 'immutable';

const DEFAULTS = {
  name: undefined,
  type: undefined
};

class Field extends Record(DEFAULTS) {
  static __field__ = true;

  static isField(field) {
    return !!field.__field__;
  }

  static create(attr = {}) {
    if (Field.isField(attr)) {
      return attr;
    }
    const { name, type } = attr;
    return new Field({ name, type });
  }

  static createList(attr = []) {
    if (Array.isArray(attr) || List.isList(attr)) {
      return List(attr.map(Field.create));
    }
    return List();
  }
}

export default Field;
